﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Customers;
using yiCommerce.Models;
using yiCommerce.Models.Customers;
using yiCommerce.Service.Authentication;
using yiCommerce.Service.Customers;
using yiCommerce.Service.Pictures;

namespace yiCommerce.Controllers
{
    public class CustomerController : BaseController
    {
        private ICustomerService _customerService;
        private IAuthenticationService _authenticationService;
        private IWorkContext _workContext;
        private IPictureService _pictureService;

        public CustomerController(ICustomerService customerService, IAuthenticationService authenticationService, IWorkContext workContext, IPictureService pictureService)
        {
            _customerService = customerService;
            _authenticationService = authenticationService;
            _workContext = workContext;
            _pictureService = pictureService;
        }

        protected bool IsCurrentUserRegistered()
        {
            return _workContext.CurrentCustomer.IsRegistered();
        }

        public ActionResult Setting()
        {
            return View();
        }
   
        public ActionResult Info()
        {
            if (!IsCurrentUserRegistered())
                return new HttpUnauthorizedResult();

            var customer = _customerService.GetCustomerById(_workContext.CurrentCustomer.Id);

            var model = new CustomerInfoEditModel()
                {
                    Id = _workContext.CurrentCustomer.Id,
                    NickName = customer.NickName,
                    Intro = customer.Intro,
                };
            ViewBag.Model = model;
            return View();
        }


        public ActionResult Logo()
        {
            var model = new CustomerLogoModel()
                {
                    PictureId = _workContext.CurrentCustomer.CustomerLogoId.HasValue? _workContext.CurrentCustomer.CustomerLogo.PictureId:0,
                    CustomerLogId = _workContext.CurrentCustomer.CustomerLogoId.HasValue? _workContext.CurrentCustomer.CustomerLogoId.Value: 0,
                    PictureUrl = _workContext.CurrentCustomer.CustomerLogoId.HasValue? _pictureService.GetPictureUrl(_workContext.CurrentCustomer.CustomerLogo.PictureId,0,true): ConstInfo.DefaultImageUrl,
                    DemoUrl = ConstInfo.DemoImageUrl,
                };
            ViewBag.Model = model;
            return View();
        }
      

        public ActionResult Login()
        {
            var model = new LoginModel();
            ViewBag.Model = model;
            return View();
        }

        public ActionResult Register()
        {
            var model = new RegisterModel();
            ViewBag.Model = model;
            return View();
        }

        public ActionResult ChangePassword()
        {
            return View();
        }

        public ActionResult SignOff()
        {
            _authenticationService.SignOut();
            return RedirectToAction("Login");
        }

    }
}
